//
//  BottomPriceView.swift
//  koolcar
//
//  Created by xiantian on 2023/12/6.
//

import UIKit

typealias BottomPriceViewBlock = (NSInteger)->Void

class BottomPriceView: UIView {
    var callBack: BottomPriceViewBlock?
    private var line: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = UIColor(hex: 0x939AA8,alpha: 0.12)
        return tempView
    }()
    
    //特价
    private var specialPriceBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.backgroundColor = Theme.redColor
        tempBtn.setTitleColor(.white, for: .normal)
        tempBtn.isUserInteractionEnabled = false
        tempBtn.layer.cornerRadius = 9
        tempBtn.clipsToBounds = true
        tempBtn.isHidden = true
        tempBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        tempBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 3, bottom: 0, right: 3)
        tempBtn.setup_titleLabel(text: MMLocalizedString("special", comment: "特价"))
        return tempBtn
    }()
    
    //总价
    private var totalPricesLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont(name: Theme.PingFangSCMedium, size: 16)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        tempLab.text = ""
        return tempLab
    }()
    
    //原价
    private var originalPriceLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = UIColor(hex: 0xAFBED7)
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont(name: Theme.PingFangSCRegular, size: 11)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        tempLab.text = ""
        return tempLab
    }()
    
    //价格说明
    private var priceTipLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = UIColor(hex: 0xAFBED7)
        tempLab.font = UIFont(name: Theme.PingFangSCRegular, size: 11)
        tempLab.textAlignment = .left
        tempLab.text = MMLocalizedString("car_detail_price_tip", comment: "税前参考价，非最终售价")
        tempLab.isHidden = false
        return tempLab
    }()
    
    //按钮背景view
    private var btnBkgView: UIView = {
        let tempView = UIView()
        tempView.layer.cornerRadius = 8
        tempView.clipsToBounds = true
        tempView.backgroundColor = UIColor(hex: 0xD2C6AE)
        return tempView
    }()
    
    ///预约看车btn
    private var seeBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setTitleColor(.white, for: .normal)
        tempBtn.setup_titleLabel(text: MMLocalizedString("see_car", comment: "预约看车"))
        tempBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCMedium, size: 16)
        return tempBtn
    }()
    
    //订购btn
    private var orderBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setTitleColor(.white, for: .normal)
        tempBtn.backgroundColor = Theme.mainColor
        tempBtn.setTitle(MMLocalizedString("order_car", comment: "订购："), for: .normal)
        tempBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCMedium, size: 16)
        return tempBtn
    }()
    
    ///是否是海外车
    var seaCarFlag: Bool = false {
        didSet {
            self.layoutBottomView()
        }
    }
    
    ///是否显示特价
    public var showSpecial: Bool? {
        didSet{
            self.specialPriceBtn.isHidden = !(self.showSpecial ?? false)
            if self.showSpecial ?? false {
                //有特价
                self.specialPriceBtn.snp.updateConstraints { make in
                    make.width.equalTo(38)
                }
                self.totalPricesLab.snp.updateConstraints { make in
                    make.left.equalTo(specialPriceBtn.snp.right).offset(4)
                }
                
            }else{
                //无特价
                self.specialPriceBtn.snp.updateConstraints { make in
                    make.width.equalTo(0)
                }
                self.totalPricesLab.snp.updateConstraints { make in
                    make.left.equalTo(specialPriceBtn.snp.right).offset(0)
                }
            }
            self.updateConstraints()
        }
    }
    
    ///总价
    public var totalPrice: Double? {
        didSet{
            let price = XTManageCenter.showPriceInComma(source: (totalPrice ?? 0).cleanZero)
            self.totalPricesLab.text = MMLocalizedString("total_price", comment: "总价: ") + XTManageCenter.currentSign() + price
        }
    }
    
    ///原价
    public var originPrice: Double? {
        didSet{
            if originPrice ?? 0 > 0 {
                let originalPrice = XTManageCenter.showPriceInComma(source: (originPrice ?? 0).cleanZero)
                let tempOriginPrice = String(format: "%@%@", XTManageCenter.currentSign(),originalPrice)
                let originPriceString = NSMutableAttributedString.init(string: tempOriginPrice)
                originPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: originPriceString.length))
                self.originalPriceLab.attributedText = originPriceString
            }else{
                self.originalPriceLab.attributedText = nil
            }
        }
    }
    
    ///订购价
    public var orderPrice: Double? {
        didSet{
            let price = XTManageCenter.showPriceInComma(source: (orderPrice ?? 0).cleanZero)
            self.orderBtn.setTitle(MMLocalizedString("order_car", comment: "訂購: ") + String(format: "%@%@",XTManageCenter.currentSign(),price), for: .normal)
        }
    }
    
    ////当前车辆状态
    /// 0: 未售出，未预订  1：已售出  2：已预订
    public var carStatus: NSInteger = 0 {
        didSet{
            if self.seaCarFlag {
                self.setBottomViewOtherCarByStatus()
            }else{
                self.setBottomViewLocalCarByStatus()
            }
        }
    }
    
    public var showPriceTipFlag: Bool? {
        didSet{
            self.priceTipLab.isHidden = !(showPriceTipFlag ?? true)
            if showPriceTipFlag ?? false {
                self.priceTipLab.snp.updateConstraints { make in
                    make.height.greaterThanOrEqualTo(22)
                }
            }else{
                self.priceTipLab.snp.updateConstraints { make in
                    make.height.greaterThanOrEqualTo(0)
                }
            }
            self.updateConstraints()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.line)
        self.addSubview(self.specialPriceBtn)
        self.addSubview(self.totalPricesLab)
        self.addSubview(self.originalPriceLab)
        self.addSubview(self.priceTipLab)
        
        self.addSubview(btnBkgView)
        btnBkgView.addSubview(self.seeBtn)
        btnBkgView.addSubview(self.orderBtn)
        self.addSnpLayout()
        
        self.seeBtn.addTarget(self, action: #selector(seeBtnAction), for: .touchUpInside)
        self.orderBtn.addTarget(self, action: #selector(orderBtnAction), for: .touchUpInside)
    }
    
    @objc func seeBtnAction(){
        self.callBack?(0)
    }
    @objc func orderBtnAction(){
        self.callBack?(1)
    }
    
    private func addSnpLayout(){
        line.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(0.5)
        }
        self.specialPriceBtn.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(14)
            make.width.equalTo(38)
            make.height.equalTo(18)
        }
        
        self.totalPricesLab.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(self.specialPriceBtn.snp.right).offset(4)
            make.height.equalTo(22)
        }
        
        self.originalPriceLab.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(self.totalPricesLab.snp.right).offset(4)
            make.right.equalTo(-14)
            make.height.equalTo(22)
        }
        
        self.priceTipLab.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(self.totalPricesLab.snp.bottom)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(22)
        }
        
        btnBkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalTo(priceTipLab.snp.bottom)
            make.bottom.equalTo(0)
        }
        
        let tempW: CGFloat = (kScreenWidth - 28)/2
        seeBtn.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(tempW)
            make.height.equalTo(46)
            make.bottom.equalTo(0)
        }
        
        orderBtn.snp.makeConstraints { make in
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(46)
            make.width.equalTo(tempW)
            
        }
        self.layoutIfNeeded()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///海外车与本地车 bottom按钮布局
    private func layoutBottomView(){
        if seaCarFlag {
            //海外车
            self.btnBkgView.backgroundColor = .white
            self.seeBtn.backgroundColor = .white
            self.seeBtn.layer.borderWidth = 1
            self.seeBtn.layer.borderColor = Theme.mainColor.cgColor
            self.seeBtn.layer.cornerRadius = 23
            self.seeBtn.layer.masksToBounds = true
            self.seeBtn.setup_titleColor(color: Theme.titleLabColor)
            self.seeBtn.setup_titleLabel(text: MMLocalizedString("car_detail_whatsapp", comment: "WhatsApp查詢"))
            
            self.orderBtn.backgroundColor = Theme.mainColor
            self.orderBtn.layer.borderWidth = 1
            self.orderBtn.layer.borderColor = Theme.mainColor.cgColor
            self.orderBtn.layer.cornerRadius = 23
            self.orderBtn.layer.masksToBounds = true
            
            let tempW: CGFloat = (kScreenWidth - 28)/2
            self.seeBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW - 10)
                make.left.equalTo(0)
            }
            self.orderBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW - 10)
                make.right.equalTo(0)
            }
            self.updateConstraints()
            self.layoutIfNeeded()
        }else{
            //本地
            self.btnBkgView.backgroundColor = UIColor(hex: 0xD2C6AE)
            self.seeBtn.layer.borderWidth = 0
            self.seeBtn.setup_titleLabel(text: MMLocalizedString("see_car", comment: "预约看车"))
            self.seeBtn.setup_titleColor(color: Theme.whiteColor)
            let tempW: CGFloat = (kScreenWidth - 28)/2
            seeBtn.snp.updateConstraints { make in
                make.left.equalTo(0)
                make.width.equalTo(tempW)
            }
            orderBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW)
            }
            self.orderBtn.roundCorners(corners: UIRectCorner(arrayLiteral: .topLeft), radius: 0)
            self.updateConstraints()
            self.layoutIfNeeded()
            self.orderBtn.roundCorners(corners: UIRectCorner(arrayLiteral: .topLeft), radius: 20)
        }
    }
    
    ///本地车订购状态
    private func setBottomViewLocalCarByStatus(){
        if carStatus == 0 {
            self.seeBtn.isUserInteractionEnabled = true
            self.seeBtn.backgroundColor = UIColor(hex: 0xD2C6AE)
            self.seeBtn.setup_titleLabel(text: MMLocalizedString("see_car", comment: "预约看车"))
            
            let tempW: CGFloat = (kScreenWidth - 28)/2
            seeBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW)
            }
            
            orderBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW)
            }
            self.orderBtn.isHidden = false
        }else if carStatus == 1 {
            let tempW: CGFloat = (kScreenWidth - 28)
            seeBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW)
            }
            
            orderBtn.snp.updateConstraints { make in
                make.width.equalTo(0)
            }
            self.seeBtn.isUserInteractionEnabled = false
            self.seeBtn.backgroundColor = UIColor(hex: 0x939AA8)
            self.seeBtn.setup_titleLabel(text: MMLocalizedString("be_sold", comment: "已出售"))
            self.orderBtn.isHidden = true
        }else{
            let tempW: CGFloat = (kScreenWidth - 28)
            seeBtn.snp.updateConstraints { make in
                make.width.equalTo(tempW)
            }
            
            orderBtn.snp.updateConstraints { make in
                make.width.equalTo(0)
            }
            self.seeBtn.isUserInteractionEnabled = false
            self.seeBtn.backgroundColor = UIColor(hex: 0x939AA8)
            self.seeBtn.setup_titleLabel(text: MMLocalizedString("scheduled", comment: "已预定"))
            self.orderBtn.isHidden = true
        }
        
        self.updateConstraints()
    }
    
    ///海外车订购状态
    private func setBottomViewOtherCarByStatus(){
        self.updateConstraints()
    }
}
